#include "Patient.h"
#include <string>
using namespace std;


Patient::Patient(void)
{
	isClean = true;

	for(int i=0; i<numVariables; i++)
	{
		variables[i] = NULL;
	}
}


Patient::~Patient(void)
{
	if(!isClean && variables[0] != NULL)
	{
		for(int i=0; i<numVariables; i++)
		{
			delete variables[i];
		}

		isClean = true;
	}
}

//Converts the patient data to a string with the selected delimiter to split the values.
string Patient::toString(char delimiter)
{
	string str = "";

	for(int i=0; i < numVariables; i++)
	{
		str += variables[i];
		str += delimiter;
	}

	return str;
}

//Set takes an char pointer and retrieves data into the variables of the patient.
bool Patient::set(char* content, char delimiter)
{
	string tmp = "";
	int i = 0, j = 0;

	while((content[i] != delimiter || j < numVariables) && i < strlen(content))
	{
		if(content[i] != delimiter)
		{
			//Prepare data
			tmp += content[i];
		}

		else
		{
			variables[j] = new char[tmp.size()+1];

			variables[j][tmp.size()]='\0';
			memcpy(variables[j],tmp.c_str(),tmp.size());

			//variables[j] = const_cast<char*>(tmp.c_str());
			tmp = "";
			j++;
		}

		i++;
	}

	isClean = false;

	return isValid();
}

//Checks if a value has been assigned to all the variables.
bool Patient::isValid()
{
	for(int i=0; i<numVariables; i++)
	{
		if(variables[i] == "" || variables[i] == NULL)
			return false;
	}

	return true;
}

//Compares a specific ID with the patient id. Returns true if equal.
bool Patient::compareId(char* id)
{
	if(variables[0] == id)
		return true;
	return false;
}

//Compares the ID with the ID from the source code. Returns true if equal.
bool Patient::compareId(char* id, char* source, char delimiter)
{
	string tmp = "";

	for(int i=0; source[i] != delimiter; i++)
	{
		tmp += source[i];
	}

	if(tmp == id && tmp.size() == strlen(id))
		return true;
	return false;
}
